package com.lanou.zakerproject.hotspot;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou3g.greendaodemo.entity.greendao.NewsDataEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/14.
 */
public class HotSpotAdapter extends BaseAdapter {
    private Context context;
    private NewsData datas;
    private List<NewsDataEntity> newsDataEntityList;//数据库
    final int VIEW_TYPE = 3;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;
    int hasNet = 1;//有网

    public HotSpotAdapter(Context context, NewsData datas, int hasNet) {
        super();
        this.context = context;
        this.datas = datas;
        this.hasNet = hasNet;
    }

    public HotSpotAdapter(Context context, List<NewsDataEntity> newsDataEntityList, int hasNet) {
        super();
        this.context = context;
        this.newsDataEntityList = newsDataEntityList;
        this.hasNet = hasNet;
    }

    @Override
    public int getCount() {
        if (hasNet == 1) {
            return datas.getData().getArticles().size();
        }
        return newsDataEntityList.size();
    }

    //每个convert view都会调用此方法，获得当前所需要的view样式
    @Override
    public int getItemViewType(int position) {
        String TYPE;
        if (hasNet == 1) {
            if (null != datas.getData().getArticles().get(position).getSpecial_info().getItem_type()) {
                TYPE = datas.getData().getArticles().get(position).getSpecial_info().getItem_type();
                if (TYPE.equals("1")) {
                    return TYPE_1;
                } else if (TYPE.equals("1_b")) {
                    return TYPE_2;
                }
            }
            return TYPE_3;
        } else {
            if (null != newsDataEntityList.get(position).getItem_type()) {
                TYPE = newsDataEntityList.get(position).getItem_type();
                if (TYPE.equals("1")) {
                    return TYPE_1;
                } else if (TYPE.equals("1_b")) {
                    return TYPE_2;
                }
            }
        }
        return TYPE_3;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public Object getItem(int position) {
        if (hasNet == 1) {
            return datas == null ? 0 : datas.getData().getArticles().get(position);
        }
        return newsDataEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyOneBigPicViewholder myOneBigPicViewHolder;
        MyOnePicViewholder myOnePicViewHolder;
        MyNoViewholder myNoViewHolder;
        int type = getItemViewType(position);
        //无convertView，需要new出各个控件
        switch (type) {
            case TYPE_1:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_hotspot_onebigpic, null);
                    myOneBigPicViewHolder = new MyOneBigPicViewholder(convertView);
                    convertView.setTag(myOneBigPicViewHolder);
                } else {
                    myOneBigPicViewHolder = (MyOneBigPicViewholder) convertView.getTag();
                }
                if (hasNet == 1) {
                    if (datas != null && datas.getData().getArticles() != null) {
                        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myOneBigPicViewHolder.oneBigPicImg, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                        VolleySingleton.getImageLoader().get(datas.getData().getArticles().get(position).getThumbnail_medias().get(0).getM_url(), listener);
                        Log.d("HotSpotAdapter", "position:" + position);
                        myOneBigPicViewHolder.tv_OneBigPicTitle.setText(datas.getData().getArticles().get(position).getTitle());
                        myOneBigPicViewHolder.tv_OneBigPicAuthor.setText(datas.getData().getArticles().get(position).getAuther_name());
                        myOneBigPicViewHolder.tv_OneBigPicDate.setText(datas.getData().getArticles().get(position).getList_dtime());
                    }
                } else {
                    myOneBigPicViewHolder.tv_OneBigPicTitle.setText(newsDataEntityList.get(position).getTitle());
                    myOneBigPicViewHolder.tv_OneBigPicAuthor.setText(newsDataEntityList.get(position).getAuther_name());
                    myOneBigPicViewHolder.tv_OneBigPicDate.setText(newsDataEntityList.get(position).getList_dtime());
                }
                break;
            case TYPE_2:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_hotspot_onepic, null);
                    myOnePicViewHolder = new MyOnePicViewholder(convertView);
                    convertView.setTag(myOnePicViewHolder);
                } else {
                    myOnePicViewHolder = (MyOnePicViewholder) convertView.getTag();
                }
                if (hasNet == 1) {
                    if (datas.getData().getArticles() != null) {
                        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myOnePicViewHolder.onePicImg, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                        VolleySingleton.getImageLoader().get(datas.getData().getArticles().get(position).getThumbnail_medias().get(0).getUrl(), listener);
                        myOnePicViewHolder.tv_OnePicTitle.setText(datas.getData().getArticles().get(position).getTitle());
                        myOnePicViewHolder.tv_OnePicAuthor.setText(datas.getData().getArticles().get(position).getAuther_name());
                        myOnePicViewHolder.tv_OnePicDate.setText(datas.getData().getArticles().get(position).getList_dtime());
                    }
                } else {
                    myOnePicViewHolder.tv_OnePicTitle.setText(newsDataEntityList.get(position).getTitle());
                    myOnePicViewHolder.tv_OnePicAuthor.setText(newsDataEntityList.get(position).getAuther_name());
                    myOnePicViewHolder.tv_OnePicDate.setText(newsDataEntityList.get(position).getList_dtime());
                }
                break;
            case TYPE_3:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_hotspot_nopic, null);
                    myNoViewHolder = new MyNoViewholder(convertView);
                    convertView.setTag(myNoViewHolder);
                } else {
                    myNoViewHolder = (MyNoViewholder) convertView.getTag();
                }
                if (hasNet == 1) {
                    if (datas.getData().getArticles() != null) {
                        myNoViewHolder.tv_noPicTitle.setText(datas.getData().getArticles().get(position).getTitle());
                        myNoViewHolder.tv_noPicAuthor.setText(datas.getData().getArticles().get(position).getAuther_name());
                        myNoViewHolder.tv_noPicDate.setText(datas.getData().getArticles().get(position).getList_dtime());
                    }
                } else {
                    myNoViewHolder.tv_noPicTitle.setText(newsDataEntityList.get(position).getTitle());
                    myNoViewHolder.tv_noPicAuthor.setText(newsDataEntityList.get(position).getAuther_name());
                    myNoViewHolder.tv_noPicDate.setText(newsDataEntityList.get(position).getList_dtime());
                }
                break;
        }
        return convertView;
    }

    class MyOnePicViewholder extends RecyclerView.ViewHolder {
        private ImageView onePicImg;
        private TextView tv_OnePicTitle, tv_OnePicAuthor, tv_OnePicDate;

        public MyOnePicViewholder(View itemView) {
            super(itemView);
            onePicImg = (ImageView) itemView.findViewById(R.id.hotspot_onePic_img);
            tv_OnePicTitle = (TextView) itemView.findViewById(R.id.news_hotspot_title);
            tv_OnePicAuthor = (TextView) itemView.findViewById(R.id.news_hotspot_author);
            tv_OnePicDate = (TextView) itemView.findViewById(R.id.news_hotspot_date);
        }
    }

    class MyOneBigPicViewholder extends RecyclerView.ViewHolder {
        private ImageView oneBigPicImg;
        private TextView tv_OneBigPicTitle, tv_OneBigPicAuthor, tv_OneBigPicDate;

        public MyOneBigPicViewholder(View itemView) {
            super(itemView);
            oneBigPicImg = (ImageView) itemView.findViewById(R.id.hotspot_oneBigPic_img);
            tv_OneBigPicTitle = (TextView) itemView.findViewById(R.id.news_hotspot_oneBigPic_title);
            tv_OneBigPicAuthor = (TextView) itemView.findViewById(R.id.news_hotspot_oneBigPic_author);
            tv_OneBigPicDate = (TextView) itemView.findViewById(R.id.news_hotspot_oneBigPic_date);
        }
    }

    class MyNoViewholder extends RecyclerView.ViewHolder {
        private TextView tv_noPicTitle, tv_noPicAuthor, tv_noPicDate;

        public MyNoViewholder(View itemView) {
            super(itemView);
            tv_noPicTitle = (TextView) itemView.findViewById(R.id.news_hotspot_noPic_title);
            tv_noPicAuthor = (TextView) itemView.findViewById(R.id.news_hotspot_noPic_author);
            tv_noPicDate = (TextView) itemView.findViewById(R.id.news_hotspot_noPic_date);
        }
    }

}
